﻿using AutoMapper;
using ManagementApi.Models.Manage.ProjectCorp;
using ManagementApi.Models.Project;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Models.Manage.ProjectGroup
{
    /// <summary>
    /// 工作组视图对象
    /// </summary>
    public class ProjectGroupDto
    {
        /// <summary>
        /// 班组ID
        ///</summary>
        public int GroupId { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string? ProjectName { get; set; }

        /// <summary>
        /// 班组名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 是否是项目管理部
        /// </summary>
        public bool? IsProjectManager { get; set; }

        /// <summary>
        /// 是否是管理组
        /// </summary>
        public bool? IsManagerGroup { get; set; }

        /// <summary>
        /// 班组ID
        ///</summary>
        public int CorpId { get; set; }

        /// <summary>
        /// 所属企业
        /// </summary>
        public string? CorpName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 进场日期
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 出场日期
        /// </summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 在场人数
        /// </summary>
        public int PersonCount { get; set; }

        public bool? HasChildren { get; set; } = true;

        /// <summary>
        /// 班组长姓名
        /// </summary>
        public string? GroupMemberLeader { get; set; }

        /// <summary>
        /// 班组长联系电话
        /// </summary>
        public string? GroupMemberLeaderMobile { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string? LinkMobile { get; set; }
    }

    public static partial class WorkGroupExceptions
    {
        public static string GetProjectGroupNameByGroupId(this Modules.Project.Entitys.ProjectGroup projectGroup)
        {
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var data = projectGroupService.GetProjectGroup(projectGroup.GroupId);
            if (data == null) return "";
            return data.Name == null ? "" : data.Name;
        }

        public static Modules.Project.Entitys.ProjectGroup GetProjectGroup(this Modules.Project.Entitys.ProjectGroup projectGroup)
        {
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var data = projectGroupService.GetProjectGroup(projectGroup.GroupId);
            if (data == null) return new Modules.Project.Entitys.ProjectGroup();
            return data;
        }

        public static string GetGroupName(this Modules.Project.Entitys.ProjectGroup projectGroup)
        {
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var data = projectGroupService.GetProjectGroup(projectGroup.GroupId);
            if (data == null) return "";
            return data.Name;
        }

        public static ProjectGroupDto AsDto(this Modules.Project.Entitys.ProjectGroup projectGroup, IApplicationContext applicationContext)
        {
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.ProjectId = projectGroup.ProjectId;
            ProjectGroupDto projectGroupDto = new ProjectGroupDto();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(projectGroup, projectGroupDto);
            projectGroupDto.ProjectName = applicationContext.GetService<ProjectService>().GetProjectInfo(projectGroup.ProjectId)?.Name;
            projectGroupDto.CorpName = applicationContext.GetService<CorpService>().GetCorpInfo(projectGroup.CorpId)?.Name;
            var projectGroupMemberService = applicationContext.GetService<ProjectGroupMemberService>();
            var list = projectGroupMemberService.GetProjectGroupMembers(new ProjectGroupMemberQuery() { GroupId = projectGroup.GroupId }, 1, 999);
            var leader = list.FirstOrDefault(i => i.IsLeader.HasValue && i.IsLeader.Value);
            if (leader != null)
            {
                var personService = applicationContext.GetService<PersonService>();
                var person = personService.GetPerson(leader.PersonId);
                projectGroupDto.GroupMemberLeader = person?.Name;
                projectGroupDto.LinkMobile = person?.Mobile;
            }
            return projectGroupDto;
        }

        public static ProjectGroupDto AsDto(this Modules.Project.Entitys.ProjectGroup projectGroup, IEnumerable<Modules.Project.Entitys.ProjectGroupMember> projectGroupMembers, IApplicationContext applicationContext)
        {
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.ProjectId = projectGroup.ProjectId;
            ProjectGroupDto projectGroupDto = new ProjectGroupDto();
            var mapper = applicationContext.GetService<IMapper>();
            mapper.Map(projectGroup, projectGroupDto);
            projectGroupDto.ProjectName = applicationContext.GetService<ProjectService>().GetProjectInfo(projectGroup.ProjectId)?.Name;
            projectGroupDto.CorpName = applicationContext.GetService<CorpService>().GetCorpInfo(projectGroup.CorpId)?.Name;
            projectGroupDto.PersonCount = projectGroupMembers.Where(a => a.GroupId == projectGroupDto.GroupId && a.IsManager == false).Count();
            return projectGroupDto;
        }

        public static IEnumerable<ProjectGroupDto> AsDtos(this IEnumerable<Modules.Project.Entitys.ProjectGroup> projectGroups, IEnumerable<Modules.Project.Entitys.ProjectGroupMember> projectGroupMembers)
        {
            List<ProjectGroupDto> projectGroupDtos = new List<ProjectGroupDto>();
            var mapper = ServiceAccessor.GetService<IMapper>();
            List<CorpInfo> corpInfos = ServiceAccessor.GetService<CorpService>().GetCorpsByCorpIds(projectGroups.Select(a => a.CorpId)).ToList();
            List<ProjectInfo> projectInfos = ServiceAccessor.GetService<ProjectService>().GetProjectInfoByProjectIds(projectGroups.Select(a => a.ProjectId)).ToList();

            var persons = ServiceAccessor.GetService<PersonService>().GetPersonsByPersonIds(projectGroupMembers.Select(a => a.PersonId));

            foreach (var item in projectGroups)
            {
                var person = persons.FirstOrDefault(i => i.PersonId == (projectGroupMembers.FirstOrDefault(a => a.GroupId == item.GroupId && a.IsLeader == true)?.PersonId ?? 0));
                ProjectGroupDto projectGroupDto = new ProjectGroupDto();
                mapper.Map(item, projectGroupDto);
                projectGroupDto.ProjectName = projectInfos.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.Name;
                projectGroupDto.CorpName = corpInfos.FirstOrDefault(a => a.CorpId == item.CorpId)?.Name;
                projectGroupDto.PersonCount = projectGroupMembers.Where(a => a.GroupId == projectGroupDto.GroupId && a.IsManager == false).Count();
                projectGroupDto.GroupMemberLeader = person?.Name;
                projectGroupDto.GroupMemberLeaderMobile = person?.Mobile;
                projectGroupDtos.Add(projectGroupDto);
            }

            return projectGroupDtos;
        }

        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="projectGroup"></param>
        /// <returns></returns>
        public static LabelValue<int> AsPullDownDto(this Modules.Project.Entitys.ProjectGroup projectGroup)
        {
            LabelValue<int> data = new LabelValue<int>();
            data.Value = projectGroup.GroupId;
            data.Label = projectGroup.Name;
            return data;
        }

        /// <summary>
        /// 班组下拉列表
        /// </summary>
        /// <param name="projectGroup"></param>
        /// <returns></returns>
        public static IEnumerable<LabelValue<int>> AsPullDownDtos(this IEnumerable<Modules.Project.Entitys.ProjectGroup> projectGroup)
        {
            //根据项目id查询劳务公司
            var corpService = ServiceAccessor.GetService<CorpService>();
            IEnumerable<CorpInfo> corps = corpService.GetCorpsByCorpIds(projectGroup.Select(i => i.CorpId));
            List<LabelValue<int>> data = new();
            foreach (var item in projectGroup)
            {
                LabelValue<int> labelValue = new LabelValue<int>();
                labelValue.Value = item.GroupId;
                labelValue.Label = item.Name + "(" + corps.FirstOrDefault(a => a.CorpId == item.CorpId)?.Name + ")";
                labelValue.Extend1 = item.IsManagerGroup;
                data.Add(labelValue);
            }

            return data;
        }

        /// <summary>
        /// 班组下拉列表
        /// </summary>
        /// <param name="projectGroup"></param>
        /// <returns></returns>
        public static IEnumerable<VueTree<int>> AsVueTree(this IEnumerable<Modules.Project.Entitys.ProjectGroup> projectGroup)
        {
            //根据项目id查询劳务公司
            var projectGroupMemberService = ServiceAccessor.GetService<ProjectGroupMemberService>();
            var projectGroupMembers = projectGroupMemberService.GetProjectGroupMembersByGroupIds(projectGroup.Select(i => i.GroupId));
            List<VueTree<int>> data = new();
            foreach (var item in projectGroup)
            {
                VueTree<int> vueTree = new()
                {
                    Value = item.GroupId,
                    Label = item.Name,
                    Id = item.GroupId,
                    leaf = true,
                    Extend1 = "Group",
                    Icon = "",
                    Extend2 = item.IsManagerGroup,
                    Extend3 = projectGroupMembers.Where(i => i.GroupId == item.GroupId).Count(),
                    Children = new List<VueTree<int>>()
                };
                data.Add(vueTree);
            }

            return data;
        }
    }
}